﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Common;
using BarCode.AccessDao;
using BarCode.Bean;
using System.Windows.Forms;
using System.IO;
using BarCode.Module;
using System.Data;

namespace BarCode.Module
{
    public class modeFunction
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(modeFunction));
        /// <summary>
        /// 上传二维码数据
        /// </summary>
        /// <param name="lineName"></param>
        /// <returns></returns>
        public static int upload_Data(string lineName)
        {
            int ret = Consts.RET_SUCCESS; 
            switch (lineName)
            {
                case "base" :
                    ret = uploadBaseData();
                    break;
                case "jk":
                    ret = uploadJkData();
                    break;
                default:
                    LogUtil.Error("未找到参数[LineName]对应的处理分支 :" + lineName);
                    ret = Consts.RET_FAIL;
                    break;
            }



            return Consts.RET_SUCCESS;
        }

        /// <summary>
        /// 上传基本二维码数据
        /// </summary>
        /// <returns></returns>
        private static int uploadBaseData()
        {
            
            BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
            // 从本地获取数据
            List<BarCodeInfo> lstBarCode = barcodeMdbDao.getBarCodeList();
            LogUtil.Info("本地二维码信息数据件数：" + lstBarCode.Count);
            
            if (lstBarCode.Count == 0)
            {
                LogUtil.Debug("本地不存在二维码信息数据，删除处理结束。");
                barcodeMdbDao.Release();
                return Consts.RET_SUCCESS;
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("请单击[确定]按钮上传数据，上传数据时请稍候…");
            }
            
            // 上传二维码信息
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            int uploadRet = db01.uploadBaseBarCode(lstBarCode);
            if (Consts.RET_SUCCESS == uploadRet)
            {
                string bakBarcodeMdb = GlobalVal.BK_PATH + "barcode_" + DateTime.Today.ToString("yyyyMMdd") + ".mdb";
                if (!File.Exists(bakBarcodeMdb))
                {
                    if (!Directory.Exists(GlobalVal.BK_PATH))
                    {
                        Directory.CreateDirectory(GlobalVal.BK_PATH);
                    }
                    // 复制文件
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb", bakBarcodeMdb);
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "Empty_barcode.mdb", AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb");
                }

                // 备份二维码数据
                int bkInsCnt = 0;
                int bkUpdCnt = 0;
                BarCodeMdbDao bkBarcodeMadbDao = new BarCodeMdbDao(bakBarcodeMdb);
                foreach (BarCodeInfo tmpBarCode in lstBarCode)
                {
                    BarCodeInfo bkBarCodeInfo = bkBarcodeMadbDao.getBarCodeById(tmpBarCode.barcodeID);
                    if (bkBarCodeInfo == null)
                    {
                        bkInsCnt += bkBarcodeMadbDao.insertBarCode(tmpBarCode);
                    }
                    else
                    {
                        bkUpdCnt += bkBarcodeMadbDao.updateBarCode(tmpBarCode);
                    }
                }
                LogUtil.Debug("备份二维码信息插入件数：" + bkInsCnt);
                LogUtil.Debug("备份二维码信息更新件数：" + bkUpdCnt);
                bkBarcodeMadbDao.Release();

                // 清空本地数据
                int delCnt = barcodeMdbDao.deleteBaseBarCode();
                barcodeMdbDao.Release();
                LogUtil.Debug("清空本地数据件数：" + delCnt);
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("数据上传完毕！");
            }
            return Consts.RET_SUCCESS;
        }


        /// <summary>
        /// 上传机壳二维码数据
        /// </summary>
        /// <returns></returns>
        private static int uploadJkData()
        {

            BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
            // 从本地获取数据
            List<JKBarCodeInfo> lstBarCode = barcodeMdbDao.getJKBarCodeList();
            LogUtil.Info("本地二维码信息数据件数：" + lstBarCode.Count);

            if (lstBarCode.Count == 0)
            {
                LogUtil.Debug("本地不存在二维码信息数据，删除处理结束。");
                barcodeMdbDao.Release();
                return Consts.RET_SUCCESS;
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("请单击[确定]按钮上传数据，上传数据时请稍候…");
            }

            // 上传二维码信息
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            int uploadRet = db01.uploadJKBarCode(lstBarCode);
            if (Consts.RET_SUCCESS == uploadRet)
            {
                string bakBarcodeMdb = GlobalVal.BK_PATH + "barcode_" + DateTime.Today.ToString("yyyyMMdd") + ".mdb";
                if (!File.Exists(bakBarcodeMdb))
                {
                    if (!Directory.Exists(GlobalVal.BK_PATH))
                    {
                        Directory.CreateDirectory(GlobalVal.BK_PATH);
                    }
                    // 复制文件
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb", bakBarcodeMdb);
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "Empty_barcode.mdb", AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb");
                }

                // 备份二维码数据
                int bkInsCnt = 0;
                int bkDelCnt = 0;
                BarCodeMdbDao bkBarcodeMadbDao = new BarCodeMdbDao(bakBarcodeMdb);
                foreach (JKBarCodeInfo tmpBarCode in lstBarCode)
                {
                    bkDelCnt += bkBarcodeMadbDao.deleteJKBarCode(tmpBarCode);
                    bkInsCnt += bkBarcodeMadbDao.insertJKBarCode(tmpBarCode);
                }
                bkBarcodeMadbDao.Release();
                LogUtil.Debug("机壳备份信息删除重复件数：" + bkDelCnt);
                LogUtil.Debug("机壳备份信息插入件数：" + bkInsCnt);

                // 清空本地数据
                int delCnt = barcodeMdbDao.deleteJKBarCode();
                barcodeMdbDao.Release();
                LogUtil.Debug("清空本地数据件数：" + delCnt);
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("数据上传完毕！");
            }
            return Consts.RET_SUCCESS;
        }

        /// <summary>
        /// 上传机芯机壳生产线数据
        /// </summary>
        /// <returns></returns>
        public static int Upload_JxJkData()
        {
            GlobalVal.bolRepeats = false;
            BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
            // 从本地获取数据
            List<JXJKBarCodeInfo> lstBarCode = barcodeMdbDao.getJxJkBarCodeList();
            LogUtil.Info("本地机芯机壳生产线数据件数：" + lstBarCode.Count);
            if (lstBarCode.Count == 0)
            {
                LogUtil.Debug("本地不存在机芯机壳生产线数据，处理结束。");
                barcodeMdbDao.Release();
                return Consts.RET_SUCCESS;
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("请单击[确定]按钮上传数据，上传数据时请稍候…");
            }

            // 删除服务器重复数据
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            int uploadRet = db01.uploadJKJXBarCode(lstBarCode);
            if (Consts.RET_SUCCESS == uploadRet)
            {
                string bakBarcodeMdb = GlobalVal.BK_PATH + "barcode_" + DateTime.Today.ToString("yyyyMMdd") + ".mdb";
                if (!File.Exists(bakBarcodeMdb))
                {
                    if (!Directory.Exists(GlobalVal.BK_PATH))
                    {
                        Directory.CreateDirectory(GlobalVal.BK_PATH);
                    }
                    // 复制文件
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb", bakBarcodeMdb);
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "Empty_barcode.mdb", AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb");
                }

                // 备份二维码数据
                int bkInsCnt = 0;
                int bkDelCnt = 0;
                BarCodeMdbDao bkBarcodeMadbDao = new BarCodeMdbDao(bakBarcodeMdb);
                foreach (JXJKBarCodeInfo tmpBarCode in lstBarCode)
                {
                    bkDelCnt += bkBarcodeMadbDao.deleteJXJKBarCode(tmpBarCode);
                    bkInsCnt += bkBarcodeMadbDao.insertJXJKBarCode(tmpBarCode);
                }
                bkBarcodeMadbDao.Release();
                LogUtil.Debug("机芯机壳备份二维码信息删除重复件数：" + bkDelCnt);
                LogUtil.Debug("机芯机壳备份二维码信息插入件数：" + bkInsCnt);

                // 清空本地数据
                int delCnt = barcodeMdbDao.deleteJXJKBarCode();
                barcodeMdbDao.Release();
                LogUtil.Debug("清空本地数据件数：" + delCnt);
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("数据上传完毕！");
            }
            return Consts.RET_SUCCESS;
        }


        /// <summary>
        /// 上传封箱条码信息
        /// </summary>
        /// <param name="intAmount"></param>
        /// <returns></returns>
        public static  bool Upload_PackData(int intAmount ) {
            BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
            DataTable dtPack = barcodeMdbDao.getPackBarCodeList();
            if (dtPack == null || dtPack.Rows.Count == 0)
            {
                LogUtil.Debug("没有需要上传的封箱条码数据.");
                return true;
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("请单击[确定]按钮上传数据，上传数据时请稍候…");
            }
            Application.DoEvents();
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            int uploadRet = db01.uploadPackBarCode(dtPack, intAmount);
            if (Consts.RET_SUCCESS == uploadRet)
            {
                string bakBarcodeMdb = GlobalVal.BK_PATH + "barcode_" + DateTime.Today.ToString("yyyyMMdd") + ".mdb";
                if (!File.Exists(bakBarcodeMdb))
                {
                    if (!Directory.Exists(GlobalVal.BK_PATH))
                    {
                        Directory.CreateDirectory(GlobalVal.BK_PATH);
                    }
                    // 复制文件
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb", bakBarcodeMdb);
                    FileHelper.CopyFile(AppDomain.CurrentDomain.BaseDirectory + "Empty_barcode.mdb", AppDomain.CurrentDomain.BaseDirectory + "barcode.mdb");
                }

                Application.DoEvents();
                BarCodeMdbDao bkBarcodeMadbDao = new BarCodeMdbDao(bakBarcodeMdb);
                int bkRet = bkBarcodeMadbDao.backupPackData(dtPack, intAmount);
                if (Consts.RET_SUCCESS == bkRet)
                {
                    LogUtil.Debug("封箱条码信息MDB备份成功.");
                    // 清空本地条码信息
                    barcodeMdbDao.deletePackBarCode();
                    LogUtil.Debug("清空本地封箱条码信息.");
                }
                else
                {
                    LogUtil.Error("封箱条码信息MDB备份失败...");
                }
                bkBarcodeMadbDao.Release();
            }
            else
            {
                return false;
            }

            if (GlobalVal.bolUploadSubMain)
            {
                MessageUtil.Info("数据上传完毕！");
            }
            barcodeMdbDao.Release();
            return true;
        }


  

    }
}
